#include "VideoContainer.h"

/* Constructor
 * Initializes the frame counter to 0.
 */
VideoContainer::VideoContainer()
{
    frameCounter = 0;
}

/* AddFrame Method
 * Takes a pointer to a IplImage (the image format used in OpenCV)
 * and makes a copy of that image, storing at the end of the videoStore
 * list.
 */
void VideoContainer::addFrame(IplImage* i)
{

    IplImage* img2 = cvCloneImage(i);    
    videoStore.append(img2);
}

/* HasNext Method
 * Returns a boolean value of true if the frame counter has not yet
 * reached the last frame stored in the videoStore.
 */
bool VideoContainer::hasNext()
{
    if(frameCounter < videoStore.size())
    {
        return true;
    }
    else
    {
        return false;
    }
}

/* ReadNext Method
 * Reads a frame from the videoStore and returns it. It then increments the
 * frame counter so that the next time readNext is called, it will return the
 * next sequential frame that was captured.
 */
IplImage* VideoContainer::readNext()
{
    if(!videoStore.isEmpty())
    {        
        IplImage* img = videoStore[frameCounter];
        frameCounter++;
        return img;
    }
    else
    {
        IplImage* img = cvCreateImage(cvSize(640,480), IPL_DEPTH_8U,3);
        std::cout << "Attempted to get a frame, but none available!" << std::endl;
        return img;
    }
}

/* ResetRead Method
 * Resets the frame counter to the beginning of the video store so that we
 * can read the recorded video from the start again.
 */
void VideoContainer::resetRead()
{
    frameCounter = 0;
}

/* Clear Method
 * Removes all the recorded frames from videoStore, effectivly deleted the
 * stored video.
 */
void VideoContainer::clear()
{
    videoStore.clear();
    frameCounter = 0;
}

/* IsEmpty Method
 * Returns true if the there is no recored video in videoStore and false
 * otherwise.
 */
bool VideoContainer::isEmpty()
{
    return videoStore.isEmpty();
}

/* Destructor
 * Clears out all the frames in the list and then calls the lists destructor
 * just to make sure that the memory has been returned.
 */
VideoContainer::~VideoContainer()
{
    videoStore.clear();
    videoStore.~QList();
}
